package org.smartercontext.contextmger.contextsphere;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import com.hp.hpl.jena.rdf.model.Model;

public class ContextSphereController {

	private EntityManager em;
	private EntityManagerFactory emf;
	
	public ContextSphereController(){
		emf=Persistence.createEntityManagerFactory("SC_CSphere");
		em=emf.createEntityManager();
	}
	
	public boolean validateUser(String username, String password){
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, username);
		
		if(cs!=null){
			if(cs.getUpassword().equals(password)){
				
				return true;
			}
		}
		
		return false;
	}
	
	public void registerNewUser(String username, String password, String name,
			String email) throws Exception{
		
		Ctxtsphere newCS = new Ctxtsphere();
		newCS.setUsername(username);
		newCS.setUpassword(password);
		
		Model userModel = ContextManagerController.createUserModel(username, email, name);
				
		byte [] userModelBytes = ContextManagerController.transformModelToBytes(userModel);
		
		newCS.setRdfgraph(userModelBytes);
		
		em.getTransaction().begin();
		em.persist(newCS);
		em.getTransaction().commit();
			
		
	}
	
	public Model getRDFGraph(String userName)throws Exception{
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, userName);
		
		if(cs!=null){
			if(cs.getRdfgraph()!=null){
				byte []rdfGraph =cs.getRdfgraph();
				Model model = ContextManagerController.readModelFromBytes(rdfGraph);
				return model;
			}
		}
		
		return null;
	}
	
	public boolean searchUsername(String userName){
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, userName);
			
		if(cs!=null){
			return false;
		}
		
		return true;
	}
	
	public byte[] getRdfGraph(String username){
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, username);
		
		if(cs!=null){
			
			byte[]rdfGraph = cs.getRdfgraph();
			return rdfGraph;			
			
		}
		return null;
		
	}
	
	public void modifyContext (byte[]model, String username){
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, username);
		
		if(cs!=null){
			cs.setRdfgraph(model);
			 em.getTransaction().begin();
			 em.merge(cs);
			 em.getTransaction().commit();
		}
		
	}
	
	public List<Permissions>listPermissionsByUser(String username){
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, username);
		
		if(cs!=null){
			return cs.getPermissionsList();
		}
		return null;
		
	}
	
	public List<Permissions>listPermissions(){
		
		TypedQuery<Permissions>query = em.createNamedQuery("Permissions.findAll", Permissions.class);
		List<Permissions>permissions = query.getResultList();
		return permissions;
		
	}
	
	public void setListUserPermissions(List<Permissions>permissions, String username){
		
		Ctxtsphere cs = em.find(Ctxtsphere.class, username);
		
		if(cs!=null){
			cs.setPermissionsList(permissions);
			em.getTransaction().begin();
			em.merge(cs);
			em.getTransaction().commit();
		}
		
	}
	
	public Permissions getPermission(Integer idPerm){
		
		Permissions perm = em.find(Permissions.class, idPerm);
		return perm;
		
	}
}
